#ifndef TRANSACTION_H
#define TRANSACTION_H

#include <list>
#include <boost/unordered_set.hpp>

#include "common.h"

enum transaction_state_t
{
	TRANSACTION_NULL,
	TRANSACTION_START,
	TRANSACTION_COMMIT,
	TRANSACTION_RESTART,
};

class transaction_t
{
	friend class transaction_object_t;
	friend class transaction_queue_t;
public:
	transaction_t();

	bool restart();
	bool committed();
	
	void start();
	void end();

	void make_irrevocable(unsigned int flag);
	void make_isolated();

	template <typename T> inline T const & read(T const & in);
	template <typename T> inline T& write(T& in);
	template <typename T> T const * read_ptr(T const * in);
	template <typename T> T* write_ptr(T* in);

private:
	boost::unordered_set<transaction_object_t*> read_set_;
	boost::unordered_set<transaction_object_t*> write_set_;

	std::list<transaction_t*>::iterator start_iter_;
	std::list<transaction_t*>::iterator end_iter_;

	transaction_state_t state_;
};

template <typename T> 
inline T const & transaction_t::read(T const & in)
{
	return in;
}

template <typename T> 
inline T& transaction_t::write(T& in)
{
	return in;
}
	
template <typename T> 
T const * transaction_t::read_ptr(T const * in)
{
	return in;
}

template <typename T> 
T* transaction_t::write_ptr(T* in)
{
	return in;
}

#endif // TRANSACTION_H
